<?php
if (!defined('admin')) {
    exit();
}
admin_page_start("多语言");
if (!MLang) {
    adminmsg("", "多语言功能未开启");
}

?>
    <h3>多语言管理</h3>
    <p>
        <select name="new_lang" id="new_lang">
            <?php
            $codeset = langGetCodeArray(false);
            foreach ($codeset as $k => $v):
                ?>
                <option value="<?php echo $k ?>"><?php echo "{$k}({$v})" ?></option>
            <?php endforeach; ?>
        </select>
        <a class="btn btn-success btn-xs" href="#" onclick="newlang()">添加新语言文件</a>
    </p>


    <table class="tableBasic" width="100%" border="0" cellpadding="8" cellspacing="0">
        <tr>
            <th>#</th>
            <th>排序</th>
            <th>图标</th>
            <th>语言名</th>
            <th>语言文件</th>
            <th>操作</th>
        </tr>
        <?php
        $langs = langGetLangFiles();
        foreach ($langs as $k => $v):
            ?>
            <tr id="tr_<?php echo $k ?>" data-file="<?php echo $v['file']; ?>">
                <td><?php echo $k + 1; ?></td>
                <td><a href="javascript:void(0)"
                       onclick="editsort(this)"><?php echo $v['sort'] ? $v['sort'] : '0'; ?></a></td>
                <td><img style="width: 20px;height: auto" src="<?php echo $v['icon']; ?>" alt=""></td>
                <td><a href="javascript:void(0)"
                       onclick="editname(this)"><?php echo $v['name'] ? $v['name'] : "未命名"; ?></a></td>
                <td><?php echo $v['file']; ?></td>
                <td>
                    <a href="javascript:void(0)" onclick="upicon(this)">上传图标</a>
                    <a href="javascript:void(0)" onclick="delicon(this)">删除图标</a>
                    <a href="javascript:void(0)" onclick="del(this)">删除</a>
                    <a href="javascript:void(0)" onclick="copy(this)">复制</a>
                    <a href="javascript:void(0)" onclick="javascript:alert('请到application/languages/中编辑')">编辑</a>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>
    <hr>
    <p style="text-align: center"><a class="btn btn-primary btn-sm" href="?do=lang_editor">去翻译</a></p>
    <script>
        function newlang() {
            var new_lang = $("#new_lang").val();
            $.post("?do=lang_save&ajax=1", {ex: "add", file: new_lang}, function (data) {
                try {
                    data = JSON.parse(data);
                    if (data && data['r'] === 1) {
                        location.reload();
                    } else {
                        alert(data['msg']);
                    }
                } catch (e) {
                    alert("服务错误");
                }
            })
        }

        function delicon(_this) {
            var $tr = $(_this).closest("tr");
            if (confirm("确定要删除图标？")) {
                $.post("?do=lang_save&ajax=1", {ex: "", file: $tr.data("file"), icon: ""}, function (data) {
                    $tr.find("img")[0].src = "";
                })
            }
        }


        function editsort(_this) {
            var $tr = $(_this).closest("tr");
            var newsort = prompt("请输入新的排序数字", $(_this).text());
            console.log(newsort);
            if (newsort !== null) {
                newsort = newsort || '0';
                $.post("?do=lang_save&ajax=1", {ex: "", file: $tr.data("file"), sort: newsort}, function (data) {
                    $(_this).text(newsort);
                })
            }
        }

        function editname(_this) {
            var $tr = $(_this).closest("tr");
            var newname = prompt("请输入新的语言名称", $(_this).text());
            if (newname !== null) {
                $.post("?do=lang_save&ajax=1", {ex: "", file: $tr.data("file"), name: newname}, function (data) {
                    $(_this).text(newname);
                })
            }
        }

        function del(_this) {
            var $tr = $(_this).closest("tr");
            if (confirm("确定要删除此语言文件？")) {
                $.post("?do=lang_save&ajax=1", {ex: "del", file: $tr.data("file")}, function (data) {
                    $tr.remove();
                })
            }
        }

        function copy(_this) {
            var $tr = $(_this).closest("tr");
            var newfilename = prompt("请输入新文件名");
            if (newfilename != null) {
                $.post("?do=lang_save&ajax=1", {
                    ex: "copy",
                    file: $tr.data("file"),
                    dfile: newfilename
                }, function (data) {
                    try {
                        data = JSON.parse(data);
                        if (data && data['r'] === 1) {
                            location.reload();
                        } else {
                            alert(data['msg']);
                        }
                    } catch (e) {
                        alert("服务错误");
                    }
                })
            }
        }

        function upicon(_this) {
            var $tr = $(_this).closest("tr");
            jtool.showOpenDialogImage(function (file) {
                jtool.readFile_file(file, function (data) {
                    if (data) {
                        $.post("?do=lang_save&ajax=1", {ex: "", file: $tr.data("file"), icon: data}, function (d) {
                            $tr.find("img")[0].src = data;
                        })

                    } else {
                        alert("请选择图片")
                    }
                })
            })
        }
    </script>
<?php
admin_page_end();